package com.leon.androidcoursedesign.ui.fragments.loginFragments;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.navigation.Navigation;

import com.leon.androidcoursedesign.R;
import com.leon.androidcoursedesign.beans.UserAccount;
import com.leon.androidcoursedesign.ui.fragments.baseFragment.BaseFragment;
import com.leon.androidcoursedesign.ui.fragments.baseFragment.BindingUtils;
import com.leon.androidcoursedesign.utils.dataBaseUtils.AccountInfoUtils;
import com.leon.androidcoursedesign.utils.dataBaseUtils.SQLiteHelperFactory;
import com.leon.androidcoursedesign.utils.dataBaseUtils.Tables;

import android.widget.Toast;

public class RegisterFragment extends BaseFragment implements View.OnClickListener {
    private EditText
            register_fragment_editText_id,
            register_fragment_editText_pwd1,
            register_fragment_editText_pwd2,
            register_fragment_editText_phone,
            register_fragment_editText_verify;

    @Override
    public int setLayout() {
        return R.layout.register_fragment;
    }

    @Override
    public void init() {
        Log.d("Leon", "init: RegisterFragment");
    }

    @Override
    public void initView(BindingUtils bindingUtils) {
        bindingUtils
                .setOnClickListener(R.id.register_fragment_imageView_back, this)
                .setOnClickListener(R.id.register_fragment_button_getVerify, this);

        register_fragment_editText_id = findView(R.id.register_fragment_editText_id);
        register_fragment_editText_pwd1 = findView(R.id.register_fragment_editText_pwd1);
        register_fragment_editText_pwd2 = findView(R.id.register_fragment_editText_pwd2);
        register_fragment_editText_phone = findView(R.id.register_fragment_editText_phone);
        register_fragment_editText_verify = findView(R.id.register_fragment_editText_verify);

        Button register_fragment_button_submit = findView(R.id.register_fragment_button_submit);
        register_fragment_button_submit.setOnClickListener(this);
    }


    @Override
    public void initValues() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_fragment_imageView_back:
                Navigation.findNavController(view).navigate(R.id.action_registerFragment_to_mainFragment);
                break;
            case R.id.register_fragment_button_getVerify:
                String id = register_fragment_editText_id.getText().toString();

                if (id.equals("")) {
                    Toast.makeText(context, "请先把账号、密码及手机号码填写完整", Toast.LENGTH_SHORT).show();
                    break;
                }

                String phone = register_fragment_editText_phone.getText().toString();
                if (phone.equals("")) {
                    Toast.makeText(context, "手机号码不填吗？", Toast.LENGTH_SHORT).show();
                    break;
                } else if (phone.length() != 11) {
                    Toast.makeText(context, "手机号码格式不正确", Toast.LENGTH_SHORT).show();
                    break;
                }

                register_fragment_editText_verify.setText("1234");
                break;
            case R.id.register_fragment_button_submit:

                String uid = register_fragment_editText_id.getText().toString();

                boolean uidState = checkUserName(uid);
                if (uidState) {
                    Toast.makeText(context, "该用户名称已被注册", Toast.LENGTH_SHORT).show();
                    break;
                }

                if (uid.equals("")) {
                    Toast.makeText(context, "ID未填写", Toast.LENGTH_SHORT).show();
                    break;
                }

                String pwd1 = register_fragment_editText_pwd1.getText().toString();
                String pwd2 = register_fragment_editText_pwd2.getText().toString();
                if (pwd1.equals("") || pwd2.equals("")) {
                    Toast.makeText(context, "密码未填写完整", Toast.LENGTH_SHORT).show();
                    break;
                }

                if (!pwd1.equals(pwd2)) {
                    Toast.makeText(context, "两次输入的密码不相同", Toast.LENGTH_SHORT).show();
                    break;
                }

                if (register_fragment_editText_verify.getText().toString().equals("")) {
                    Toast.makeText(context, "验证码未填写", Toast.LENGTH_SHORT).show();
                    break;
                }

                UserAccount userAccount = new UserAccount();
                userAccount.uid = register_fragment_editText_id.getText().toString();
                userAccount.pwd = pwd1;
                userAccount.phone = register_fragment_editText_phone.getText().toString();
                boolean registerState = registerAccount(userAccount);

                if (registerState) {
                    Toast.makeText(context, "注册成功", Toast.LENGTH_SHORT).show();

                    //延迟跳转页面
                    Handler handler = new Handler();
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Bundle bundle = new Bundle();
                            bundle.putString("uid", userAccount.uid);
                            bundle.putString("pwd", userAccount.pwd);
                            Navigation.findNavController(view).navigate(R.id.action_registerFragment_to_mainFragment, bundle);
                        }
                    }, 1000);
                } else {
                    Toast.makeText(context, "注册失败", Toast.LENGTH_SHORT).show();
                    break;
                }

                break;
            default:
                break;
        }
    }

    /**
     * 查询是否有同名
     *
     * @param userName  注册的uid
     * @return  返回存在状态
     */
    private boolean checkUserName(String userName) {
        SQLiteHelperFactory sqLiteHelperFactory = new SQLiteHelperFactory(context, Tables.AccountInfo);
        AccountInfoUtils accountInfoUtils = (AccountInfoUtils) sqLiteHelperFactory.getInstance();
        boolean state = accountInfoUtils.queryUser(userName);

        accountInfoUtils.close();

        return state;
    }

    /**
     * 注册用户
     *
     * @param userAccount   userAccount
     * @return  返回注册状态
     */
    private boolean registerAccount(UserAccount userAccount) {
        SQLiteHelperFactory sqLiteHelperFactory = new SQLiteHelperFactory(context, Tables.AccountInfo);
        AccountInfoUtils accountInfoUtils = (AccountInfoUtils) sqLiteHelperFactory.getInstance();
        boolean state = accountInfoUtils.addAccountInfo(userAccount);

        accountInfoUtils.close();

        return state;
    }
}
